package cn.jinjimi.app.main.widget;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.baiiu.filter.util.UIUtil;
import com.facebook.drawee.drawable.ScalingUtils;
import cn.jinjimi.base.util.AndroidUtils;
import cn.jinjimi.app.R;
import cn.jinjimi.app.main.widget.FrescoImageView.FrescoImageView;
import cn.jinjimi.app.util.StatusbarUtils;

/**
 * Created by lforxeverc on 2016/10/24.
 */

public class HomeGuideView extends RelativeLayout implements View.OnClickListener {
    private FrescoImageView imageView;
    private ImageView ivClose;
    private OnGuideViewClickListener mListener;
    private int translationY;

    public HomeGuideView(Context context) {
        super(context);
        init();
    }

    public HomeGuideView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public HomeGuideView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        try {
            setBackgroundColor(Color.TRANSPARENT);
            imageView = new FrescoImageView(getContext());
            imageView.setActualImageScaleType(ScalingUtils.ScaleType.FIT_XY);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setOnClickListener(this);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
            addView(imageView, lp);
            ivClose = new ImageView(getContext());
            ivClose.setImageResource(R.drawable.icon_close_gray);
            ivClose.setScaleType(ImageView.ScaleType.FIT_XY);
            ivClose.setOnClickListener(this);
            int size = UIUtil.dp(getContext(), 44);
            RelativeLayout.LayoutParams lpClose = new RelativeLayout.LayoutParams(size, size);
            lpClose.addRule(ALIGN_PARENT_RIGHT);
            lpClose.addRule(ALIGN_PARENT_TOP);
            int padding = UIUtil.dp(getContext(), 12);
            ivClose.setPadding(padding, padding, padding, padding);

            if (AndroidUtils.hasKitKat()) {
                int statusBarHeight = StatusbarUtils.getStatusBarHeight(getContext());
                lpClose.topMargin += statusBarHeight;
            }

            addView(ivClose, lpClose);
        } catch (OutOfMemoryError e) {
            setVisibility(View.GONE);
        } finally {
            setVisibility(View.GONE);
        }
    }

    public void setListener(OnGuideViewClickListener l) {
        mListener = l;
    }

    @Override
    public void onClick(View v) {
        if (mListener == null) return;
        if (v == imageView) {
            mListener.onClickGuideView();
        } else if (v == ivClose) {
            mListener.onClickClose();
        }
    }

    public void show() {
        this.setVisibility(View.VISIBLE);
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "translationY", -translationY, 0);
        objectAnimator.setDuration(500);
        objectAnimator.start();

    }

    public void hide() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(this, "translationY", 0, -translationY);
        objectAnimator.setDuration(500);
        objectAnimator.start();
    }

    public void setImageViewBitmap(Bitmap bm) {
        int h = bm.getHeight();
        int w = bm.getWidth();
        imageView.setImageBitmap(bm);
        int realH = getResources().getDisplayMetrics().widthPixels * h / w;
        getLayoutParams().height = realH;
        translationY = realH + UIUtil.dp(getContext(), 5);

    }

    public interface OnGuideViewClickListener {

        void onClickGuideView();

        void onClickClose();
    }


}
